﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using Jalal.Library.Utility;
using BCGC.BLL.BusinessObject.Resource;
using BCGC.BLL.BusinessObject;
using System.Collections.Generic;
using System.Drawing;

public partial class Admin_Resource_Wallpaper_EditWallpaper : AdminBasePage
{
    private int Id
    {
        get
        {
            if (ViewState["Id"] == null)
                ViewState["Id"] = 0;
            return (int)ViewState["Id"];
        }
        set { ViewState["Id"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Id = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
            {
                Id = id;
                LoadData();
            }
            else
                Response.Redirect(CustomSiteMap.Wallpapers);
        }
    }

    private void LoadData()
    {

        ddlGameTitle.DataSource = Game.GetAllGames();
        ddlGameTitle.DataTextField = "GameTitle";
        ddlGameTitle.DataValueField = "GameId";
        ddlGameTitle.DataBind();

        ddlGameTitle.Items.Add(new ListItem("Create New Game", "NewGame"));
        ddlGameTitle.Items.Insert(0, new ListItem("Select Game", "-1"));
        ddlGameTitle.SelectedIndex = 0;

        pnlAddGame.Visible = false;
        tbxNewGame.Text = string.Empty;

        Wallpaper wallpaper = Wallpaper.GetWallpaperById(Id);
        if (wallpaper == null)
            Response.Redirect(CustomSiteMap.Wallpapers);
        else
        {
            tbxTitle.Text = wallpaper.Title;
            ddlGameTitle.SelectedValue = wallpaper.RelatedGame.GameId.ToString();
            imgPreview.ImageUrl = wallpaper.ThumbnailFileUrl;
            fckDescription.Value = Server.HtmlDecode(wallpaper.Description);
            cbxActive.Checked = wallpaper.IsActive;
        }
    }

    protected void AddNewGame_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (Game.Create(Security.BindStringInput(tbxNewGame.Text, SecurityLevel.Three)))
            {
                uccResponse.SetSuccessMessage("Game successfuly created");
                LoadData();
            }
            else
                uccResponse.SetFailMessage("Failed to create game");
        }
    }

    protected void GameTitle_Changed(object sender, EventArgs e)
    {
        if (ddlGameTitle.SelectedValue == "NewGame")
            pnlAddGame.Visible = true;
        else
            pnlAddGame.Visible = false;
    }

    protected void Update_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            List<Size> sizes = new List<Size>();
            sizes.Add(new Size(100, 0));
            sizes.Add(new Size(800, 600));

            uccImageUploader.Sizes = sizes;
            List<string> imageNames = uccImageUploader.UploadImage();

            if (Wallpaper.Update(Id, Security.BindStringInput(tbxTitle.Text, SecurityLevel.Three),
                Security.BindIntegerInput(ddlGameTitle.SelectedValue, SecurityLevel.Three),
                Security.BindStringInput(fckDescription.Value, SecurityLevel.Three),
                Security.BindStringInput(imageNames[0], SecurityLevel.Three),
                "800x600", Security.BindStringInput(imageNames[1], SecurityLevel.Three),
                cbxActive.Checked))
            {
                uccResponse.SetSuccessMessage("Wallpaper successfuly updated");
                ClearInput();
            }
            else
                uccResponse.SetFailMessage("Failed to upadate wallpaper");
        }
    }

    private void ClearInput()
    {
        tbxTitle.Text = string.Empty;
        ddlGameTitle.SelectedIndex = 0;
        fckDescription.Value = string.Empty;
        cbxActive.Checked = false;
    }
}
